﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frm_partyMaster : Form
    {
        OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "/PartyDetail.accdb");
        string command;
        public frm_partyMaster()
        {
            InitializeComponent();
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            if (command == "add")
            {
                conn.Open();
                string Query = "insert into Detail(PartyId,Firmname,Gender,partyname,Address,city,dist,state,phone,landline,fax,website,email,Partynote,cst,gst,tin,blacklist,creditterm,creditlimit) values (@Id,@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,'" + cbxblacklist.Checked + "',@d18,@d19)";
                OleDbCommand cmd = new OleDbCommand(Query, conn);
                cmd.Parameters.AddWithValue("@Id", txtPartyId.Text);
                cmd.Parameters.AddWithValue("@d1", txtfirmname.Text);
                cmd.Parameters.AddWithValue("@d2", dropbox.Text);
                cmd.Parameters.AddWithValue("@d3", txtname.Text);
                cmd.Parameters.AddWithValue("@d4", txtAddress.Text);
                cmd.Parameters.AddWithValue("@d5", txtcity.Text);
                cmd.Parameters.AddWithValue("@d6", txtdist.Text);
                cmd.Parameters.AddWithValue("@d7", txtstate.Text);
                cmd.Parameters.AddWithValue("@d8", txtphone.Text);
                cmd.Parameters.AddWithValue("@d9", txtlandline.Text);
                cmd.Parameters.AddWithValue("@d10", txtfax.Text);
                cmd.Parameters.AddWithValue("@d11", txtwebsite.Text);
                cmd.Parameters.AddWithValue("@d12", txtemail.Text);
                cmd.Parameters.AddWithValue("@d13", txtnote.Text);
                cmd.Parameters.AddWithValue("@d14", txtcst.Text);
                cmd.Parameters.AddWithValue("@d15", txtgst.Text);
                cmd.Parameters.AddWithValue("@d16", txttin.Text);
                cmd.Parameters.AddWithValue("@d18", txtcterm.Text);
                cmd.Parameters.AddWithValue("@d19", txtclimit.Text);
                cmd.ExecuteNonQuery();
                conn.Close();
                command = "";
            }
            else
            {
                conn.Open();
                string Query = "update Detail set Firmname=@d1,Gender=@d2,partyname=@d3,Address=@d4,city=@d5,dist=@d6,state=@d7,phone=@d8,landline=@d9,fax=@d10,website=@d11,email=@d12,Partynote=@d13,cst=@d14,gst=@d15,tin=@d16,blacklist='" + cbxblacklist.Checked + "',creditterm=@d18,creditlimit=@19 where PartyId=@d20";
                OleDbCommand cmd = new OleDbCommand(Query, conn);
                cmd.Parameters.AddWithValue("@d1", txtfirmname.Text);
                cmd.Parameters.AddWithValue("@d2", dropbox.Text);
                cmd.Parameters.AddWithValue("@d3", txtname.Text);
                cmd.Parameters.AddWithValue("@d4", txtAddress.Text);
                cmd.Parameters.AddWithValue("@d5", txtcity.Text);
                cmd.Parameters.AddWithValue("@d6", txtdist.Text);
                cmd.Parameters.AddWithValue("@d7", txtstate.Text);
                cmd.Parameters.AddWithValue("@d8", txtphone.Text);
                cmd.Parameters.AddWithValue("@d9", txtlandline.Text);
                cmd.Parameters.AddWithValue("@d10", txtfax.Text);
                cmd.Parameters.AddWithValue("@d11", txtwebsite.Text);
                cmd.Parameters.AddWithValue("@d12", txtemail.Text);
                cmd.Parameters.AddWithValue("@d13", txtnote.Text);
                cmd.Parameters.AddWithValue("@d14", txtcst.Text);
                cmd.Parameters.AddWithValue("@d15", txtgst.Text);
                cmd.Parameters.AddWithValue("@d16", txttin.Text);
                cmd.Parameters.AddWithValue("@d18", txtcterm.Text);
                cmd.Parameters.AddWithValue("@d19", txtclimit.Text);
                cmd.Parameters.AddWithValue("@d20", txtPartyId.Text);
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Data Successfully Updated");
                command = "";
            }
            disable();
            btnadd.Enabled = true;
            btnadd.Focus();
            btvupdate.Enabled = true;
            btndelete.Enabled = true;
            // btnprint.Enabled = true;
            btnsave.Enabled = false;
        }

        private void btvupdate_Click_1(object sender, EventArgs e)
        {
            if (command == "add")
            {
                txtfirmname.Enabled = true;
                txtfirmname.Focus();
            }
            else
            {
                clear();
                txtPartyId.Enabled = true;
                txtPartyId.Focus();
            }
            command = "edit";
        }

        private void btndelete_Click_1(object sender, EventArgs e)
        {
            if (command == null)
            {
                command = "delete";
                txtPartyId.Enabled = true;
                txtPartyId.Focus();
            }
            else
            {
                command = "delete";
                MessageBox.Show("Are you sure you want to delete this data", "DELETE");
                conn.Open();
                string Query = "delete from Detail where partyId=@d1 and Firmname=@d2";
                OleDbCommand cmd = new OleDbCommand(Query, conn);
                cmd.Parameters.AddWithValue("@d1", txtPartyId.Text);
                cmd.Parameters.AddWithValue("@d2", txtfirmname.Text);
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Data Successfully Deleted");
                clear();
                txtPartyId.Clear();
                btnadd.Focus();
            }

        }
        public void disable()
        {
            txtPartyId.Enabled = false;
            txtname.Enabled = false;
            txtfirmname.Enabled = false;
            txtAddress.Enabled = false;
            txtcity.Enabled = false;
            txtdist.Enabled = false;
            txtstate.Enabled = false;
            txtphone.Enabled = false;
            txtlandline.Enabled = false;
            txtfax.Enabled = false;
            txtwebsite.Enabled = false;
            txtemail.Enabled = false;
            txtnote.Enabled = false;
            txtcst.Enabled = false;
            txtgst.Enabled = false;
            txttin.Enabled = false;
            txtcterm.Enabled = false;
            txtclimit.Enabled = false;
            dropbox.Enabled = false;
            cbxblacklist.Enabled = false;
            btnsave.Enabled = false;
            //btnprint.Enabled = false;
            btncancel.Enabled = false;
        }
        public void next(KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                if (this.ActiveControl != null)
                {
                    this.SelectNextControl(this.ActiveControl, true, true, true, true);
                }
                e.Handled = true;
            }
        }

        public void dataget()
        {
            conn.Open();
            string Query = "select * from Detail where PartyId=" + txtPartyId.Text + "";
            OleDbCommand cmd = new OleDbCommand(Query, conn);
            if (txtPartyId.Text == string.Empty)
            {
                MessageBox.Show("Please Enter a PartyId");
                txtfirmname.Enabled = false;
            }
            else
            {
                OleDbDataReader rdr = cmd.ExecuteReader();
                if (rdr.Read() == true)
                {
                    txtfirmname.Text = rdr.GetString(1);
                    dropbox.Text = rdr.GetString(2);
                    txtname.Text = rdr.GetString(3);
                    txtAddress.Text = rdr.GetString(4);
                    txtcity.Text = rdr.GetString(5);
                    txtdist.Text = rdr.GetString(6);
                    txtstate.Text = rdr.GetString(7);
                    txtphone.Text = rdr.GetString(8);
                    txtlandline.Text = rdr.GetString(9);
                    txtfax.Text = rdr.GetString(10);
                    txtwebsite.Text = rdr.GetString(11);
                    txtemail.Text = rdr.GetString(12);
                    txtnote.Text = rdr.GetString(13);
                    txtcst.Text = rdr.GetString(14);
                    txtgst.Text = rdr.GetString(15);
                    txttin.Text = rdr.GetString(16);
                    cbxblacklist.Checked = Convert.ToBoolean(rdr.GetString(17));
                    txtcterm.Text = rdr.GetString(18);
                    txtclimit.Text = rdr.GetString(19);
                    txtfirmname.Enabled = false; ;
                    btnadd.Enabled = true;
                    btndelete.Enabled = true;
                    btncancel.Enabled = true;
                    if (command == "add")
                    {
                        btvupdate.Enabled = true;
                        btvupdate.Focus();

                    }
                    else
                    {
                        txtfirmname.Enabled = true;
                        txtfirmname.Focus();
                    }
                }

                else
                {
                    if (command == "add")
                    {
                        txtfirmname.Enabled = true;
                        txtfirmname.Focus();
                    }
                    else
                    {
                        MessageBox.Show("Enter a valid PartyId");
                        txtPartyId.Focus();
                    }
                }

            }
            conn.Close();
        }
        private void txtPartyId_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar == 27)
            {
                partydatagrid.Visible = false;

            }
            else if (e.KeyChar == 13)
            {
                clear();
                txtfirmname.Enabled = true;
                dataget();
            }
            else if (e.KeyChar == 32)
            {
                txtfirmname.Enabled = true;
                txtfirmname.Focus();
                string Query = "select * from detail";
                loadDataGrid(Query);
                partydatagrid.Visible = true;

            }

        }
        private void txtfirmname_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                partydatagrid.Visible = false;
            }
            else if (e.KeyChar == 27)
            {
                partydatagrid.Visible = false;
            }
            dropbox.Enabled = true;
            next(e);
        }
        private void txtname_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtAddress.Enabled = true;
            next(e);
        }
        private void dropbox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtname.Enabled = true;
                txtname.Focus();

            }
        }

        private void txtAddress_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtcity.Enabled = true;
            next(e);
        }

        private void txtcity_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtdist.Enabled = true;
            next(e);
        }

        private void txtdist_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtstate.Enabled = true;
            next(e);
        }

        private void txtstate_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtphone.Enabled = true;
            next(e);
        }

        private void txtphone_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtlandline.Enabled = true;
            next(e);
        }

        private void txtlandline_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtfax.Enabled = true;
            next(e);
        }

        private void txtfax_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtwebsite.Enabled = true;
            next(e);
        }

        private void txtwebsite_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtemail.Enabled = true;
            next(e);
        }

        private void txtemail_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtnote.Enabled = true;
            next(e);
        }

        private void txtnote_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtcst.Enabled = true;
            next(e);
        }

        private void txtcst_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtgst.Enabled = true;
            next(e);
        }

        private void txtgst_KeyPress(object sender, KeyPressEventArgs e)
        {

            txttin.Enabled = true;
            next(e);
        }

        private void txttin_KeyPress(object sender, KeyPressEventArgs e)
        {

            cbxblacklist.Enabled = true;
            next(e);
        }

        private void cbxblacklist_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtcterm.Enabled = true;
            next(e);
        }

        private void txtcterm_KeyPress(object sender, KeyPressEventArgs e)
        {

            txtclimit.Enabled = true;
            next(e);
        }

        private void txtclimit_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnsave.Enabled = true;
                btnsave.Focus();
                // btnprint.Enabled = true;
                btncancel.Enabled = true;


            }

        }

        public void loadDataGrid(string sqlQueryString)
        {

            OleDbCommand SQLQuery = new OleDbCommand();
            DataTable data = null;
            partydatagrid.DataSource = null;
            SQLQuery.Connection = null;
            OleDbDataAdapter dataAdapter = null;
            partydatagrid.Columns.Clear(); // <-- clear columns

            SQLQuery.CommandText = sqlQueryString;
            SQLQuery.Connection = conn;
            data = new DataTable();
            dataAdapter = new OleDbDataAdapter(SQLQuery);
            dataAdapter.Fill(data);
            partydatagrid.DataSource = data;
            partydatagrid.AllowUserToAddRows = false; // remove the null line
            partydatagrid.ReadOnly = true;
            foreach (DataGridViewRow row in partydatagrid.Rows)
            {
                row.Height = 20;
                row.DefaultCellStyle.Font = new Font("VDGGJ", 15);
            }
            partydatagrid.Columns[0].Width = 80;
            partydatagrid.Columns[1].Width = 130;
            partydatagrid.Columns[3].Width = 130;
            partydatagrid.Columns[2].Visible = false;
            partydatagrid.Columns[4].Visible = false;
            partydatagrid.Columns[5].Visible = false;
            partydatagrid.Columns[6].Visible = false;
            partydatagrid.Columns[7].Visible = false;
            partydatagrid.Columns[8].Visible = false;
            partydatagrid.Columns[9].Visible = false;
            partydatagrid.Columns[10].Visible = false;
            partydatagrid.Columns[11].Visible = false;
            partydatagrid.Columns[12].Visible = false;
            partydatagrid.Columns[13].Visible = false;
            partydatagrid.Columns[14].Visible = false;
            partydatagrid.Columns[15].Visible = false;
            partydatagrid.Columns[16].Visible = false;
            partydatagrid.Columns[17].Visible = false;
            partydatagrid.Columns[18].Visible = false;
            partydatagrid.Columns[19].Visible = false;

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            partydatagrid.Visible = false;
            disable();
            btnadd.Focus();
            setall();
            frm_mainentry frm = new frm_mainentry();


        }
        public void Enable()
        {
            txtPartyId.Enabled = true;
            txtname.Enabled = true;
            txtfirmname.Enabled = true;
            txtAddress.Enabled = true;
            txtcity.Enabled = true;
            txtdist.Enabled = true;
            txtstate.Enabled = true;
            txtphone.Enabled = true;
            txtlandline.Enabled = true;
            txtfax.Enabled = true;
            txtwebsite.Enabled = true;
            txtemail.Enabled = true;
            txtnote.Enabled = true;
            txtcst.Enabled = true;
            txtgst.Enabled = true;
            txttin.Enabled = true;
            txtcterm.Enabled = true;
            txtclimit.Enabled = true;
            dropbox.Enabled = true;
            cbxblacklist.Enabled = true;

        }

        public void clear()
        {
            // txtPartyId.Clear();
            txtname.Clear();
            txtfirmname.Clear();
            txtAddress.Clear();
            txtcity.Clear();
            txtdist.Clear();
            txtstate.Clear();
            txtphone.Clear();
            txtlandline.Clear();
            txtfax.Clear();
            txtwebsite.Clear();
            txtemail.Clear();
            txtnote.Clear();
            txtcst.Clear();
            txtgst.Clear();
            txttin.Clear();
            txtcterm.Clear();
            txtclimit.Clear();
            cbxblacklist.Checked = false; ;
        }

        private void btnadd_Click(object sender, EventArgs e)
        {

            command = "add";


            int a;
            clear();
            txtPartyId.Clear();
            disable();
            txtPartyId.Enabled = true;
            btnadd.Enabled = false;
            btncancel.Enabled = true;
            btvupdate.Enabled = false;
            btndelete.Enabled = false;
            conn.Open();

            string Query = "select MAX(PartyId) from Detail";
            OleDbCommand cmd = new OleDbCommand(Query, conn);
            cmd.ExecuteScalar();
            string result = cmd.ExecuteScalar().ToString();
            if (result == "")
            {
                txtPartyId.Text = "1";

            }
            else
            {
                a = Convert.ToInt32(cmd.ExecuteScalar());
                txtPartyId.Text = (a + 1).ToString();
            }
            conn.Close();
            txtPartyId.Focus();

        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            btnadd.Focus();
            clear();
            txtPartyId.Clear();
            btnadd.Enabled = true;
            Form1_Load(sender, e);

        }

        private void btnadd_Enter(object sender, EventArgs e)
        {
            btnadd.BackColor = Color.SandyBrown;
        }

        private void btnadd_Leave(object sender, EventArgs e)
        {
            btnadd.BackColor = Color.SeaShell;
        }

        private void btvupdate_Enter(object sender, EventArgs e)
        {
            btvupdate.BackColor = Color.SandyBrown;

        }

        private void btnsave_Enter(object sender, EventArgs e)
        {
            btnsave.BackColor = Color.SandyBrown;
        }

        private void btncancel_Enter(object sender, EventArgs e)
        {
            btncancel.BackColor = Color.SandyBrown;
        }

        private void btndelete_Enter(object sender, EventArgs e)
        {
            btndelete.BackColor = Color.SandyBrown;
        }

        private void btnview_Enter(object sender, EventArgs e)
        {
            btnview.BackColor = Color.SandyBrown;
        }

        private void btnprint_Enter(object sender, EventArgs e)
        {
            //btnprint.BackColor = Color.SandyBrown;
        }

        private void btvupdate_Leave(object sender, EventArgs e)
        {
            btvupdate.BackColor = Color.SeaShell;
        }

        private void btnsave_Leave(object sender, EventArgs e)
        {
            btnsave.BackColor = Color.SeaShell;
        }

        private void btncancel_Leave(object sender, EventArgs e)
        {
            btncancel.BackColor = Color.SeaShell;
        }

        private void btndelete_Leave(object sender, EventArgs e)
        {
            btndelete.BackColor = Color.SeaShell;
        }

        private void btnview_Leave(object sender, EventArgs e)
        {
            btnview.BackColor = Color.SeaShell;
        }

        private void btnprint_Leave(object sender, EventArgs e)
        {
            //btnprint.BackColor = Color.SeaShell;
        }

        private void txtPartyId_Enter(object sender, EventArgs e)
        {
            txtPartyId.Focus();
            txtPartyId.SelectionStart = txtPartyId.Text.Length;
            partydatagrid.Visible = false;

        }
        public void setall()
        {
            foreach (Control item in Controls)
            {
                if (item is TextBox)
                {
                    var textbox = (TextBox)item;
                    textbox.Font = new Font(textbox.Font.FontFamily, 8);
                }
            }

        }
        private void txtfirmname_Leave(object sender, EventArgs e)
        {
            btnsave.Enabled = true;
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 27)
            {
                partydatagrid.Visible = false;
            }
        }

        private void txtfirmname_TextChanged(object sender, EventArgs e)
        {

            string Query = "select * from detail where Firmname like '" + txtfirmname.Text + "%'";
            loadDataGrid(Query);
        }

        private void txtPartyId_TextChanged(object sender, EventArgs e)
        {
            {
                Exception X = new Exception();

                TextBox T = (TextBox)sender;

                try
                {
                    if (T.Text != "-")
                    {
                        int x = int.Parse(T.Text);
                    }
                }
                catch (Exception)
                {
                    try
                    {
                        int CursorIndex = T.SelectionStart - 1;
                        T.Text = T.Text.Remove(CursorIndex, 1);
                        T.SelectionStart = CursorIndex;
                        T.SelectionLength = 0;
                    }
                    catch (Exception) { }
                }
            }
        }

        private void txtphone_TextChanged(object sender, EventArgs e)
        {
            {
                Exception X = new Exception();

                TextBox T = (TextBox)sender;

                try
                {
                    if (T.Text != "-")
                    {
                        int x = int.Parse(T.Text);
                    }
                }
                catch (Exception)
                {
                    try
                    {
                        int CursorIndex = T.SelectionStart - 1;
                        T.Text = T.Text.Remove(CursorIndex, 1);
                        T.SelectionStart = CursorIndex;
                        T.SelectionLength = 0;
                    }
                    catch (Exception) { }
                }
            }
        }

        private void partydatagrid_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar == 13)
            {
                int i = partydatagrid.CurrentRow.Index;
                string data = partydatagrid[0, i].Value.ToString();
                txtPartyId.Text = data;
                txtPartyId.Focus();
                txtfirmname.Enabled = false;

            }
        }

        private void txtfirmname_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                partydatagrid.Focus();
            }

        }

        private void partydatagrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                e.Handled = true;
            }
        }

        private void gboxparty_Enter(object sender, EventArgs e)
        {

        }

        private void gboxadd_Enter(object sender, EventArgs e)
        {

        }
        public string ModifyTextBoxValue
        {
            //frm_mainentry frm = new frm_mainentry();

            get { return label1.Text; }
            set { label1.Text = value; }
        }

        private void btnexit_Click(object sender, EventArgs e)
        {

                this.Hide();
        }

    }


}
